from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse
import json

from util.exception import ValidationError
from django.conf import settings
import traceback


class Md1(MiddlewareMixin):
    def process_exception(self, request, exception):
        settings.LOGGER.error(traceback.format_exc())
        if isinstance(exception, ValidationError):
            msg = ValidationError.get_msg(exception.code)
            return_msg = {"code": exception.code, "msg": msg}
            return HttpResponse(json.dumps(return_msg), content_type="application/json")
        else:
            pass
